/*
 * Title                       Menu System 
 * Description                 This program is Thomson's Door-to-Door System menu
 * Author                      Thomson
 * Date 19 February            2017
 * Version History ----------- Version 1.0 19 February 2017
 * Version History ----------- Version 2.0 05 march 2017
  *Version History ----------- Version 3.5 12 march 2017
 */
package cn.wangsy.zhidao.p20170408;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

public class MenuSystem {

	public static Prospect[] data = new Prospect[20];
	static Scanner input = new Scanner(System.in);
	static String option = "";
	String temp;

	public static void main(String[] args) {

		boolean doLoop = true;

		loadData();

		// loop around

		while (doLoop) {
			drawMenu();
			option = input.nextLine();
			System.out.printf("I am doing %s \n", option.toUpperCase());

			if (option.toUpperCase().equals("A")) {
				addCustomer();
			} else if (option.toUpperCase().equals("E")) {
				editCustomer();
			} else if (option.toUpperCase().equals("L")) {
				listCustomer();
			} else if (option.toUpperCase().equals("D")) {
				deleteCustomer();
			} else if (option.toUpperCase().equals("Q")) {
				doLoop = false;
				System.out.println("Quit");
			} else {
				System.out.println("You are enter error words");
			}
			// print eeor message
		}
		saveData();
	}

	public static void drawMenu() {

		System.out.println("Main Menu");
		System.out.println("_____________");
		System.out.println("A Add Customer");
		System.out.println("E Edit Customer");
		System.out.println("L List Customer");
		System.out.println("D Delete Customer");
		System.out.println("Q Quit");
		System.out.printf("Enter an option:    ");
	}

	public static void addCustomer() {
		System.out.println("I am Adding Customer");
		//option = input.nextLine();
		// 先判断数组是否已满
		int index = 0;
		for(;index < data.length;index++){
			if(data[index] == null){
				break;
			}
		}
		if(index == data.length){
			System.err.println("data is full!");
			return;
		}
		
		System.out.println("name:");
		String name = input.nextLine();
		System.out.println("houseNumber:");
		String houseNumber = input.nextLine();
		System.out.println("postcode:");
		String postcode = input.nextLine();
		System.out.println("dateContacted:");
		String dateContacted = input.nextLine();
		
		Prospect prospect = new Prospect();
		prospect.setName(name);
		prospect.setHouseNumber(houseNumber);
		prospect.setPostcode(postcode);
		prospect.setDateContacted(dateContacted);
		//赋值，保存数据
		data[index] = prospect;
	}

	public static void editCustomer() {
      String Name;
      String Housenumber;
      String Postcode;
      String Datecontacted;
		System.out.println("I am editing customer");
          System.out.println("Please enter which one do you want edit:  ");
		option = input.nextLine();
		try {
			int optionInt = Integer.parseInt(option);
			if (optionInt >= 0 && optionInt < data.length) {
				
				//Prospect[] tempData = new Prospect[data.length-1];
				Prospect curProspect = data[optionInt];
				
				System.out.println("please enter new name");
                Name = input.nextLine();
                curProspect.setName(Name);
                System.out.println("please enter new house number");
                Housenumber = input.nextLine();
                curProspect.setHouseNumber(Housenumber);
                System.out.println("please enter new postcode");
                Postcode = input.nextLine();
                curProspect.setPostcode(Postcode);
                System.out.println("please enter new date contact");
                Datecontacted = input.nextLine();
                curProspect.setDateContacted(Datecontacted);
                System.out.printf(", %-8s, %5s, %-10s,%8s\n","Name","Housenumber","Postcode","Datecontacted");
                //next step, set value back to data
					
				System.out.println("deit complete");
			}else{
				System.err.println("Error:Number is out of range");
			}
		} catch (NumberFormatException e) {
			System.err.println("Error:It is not Number");
		}
		//option = input.nextLine();
	}

	public static void listCustomer() {
		System.out.println("I am listing customer");
        System.out.printf("Number, %-8s, %5s, %-10s,%8s\n","Name","House Number","Postcode","DateContacted");
		for (int i = 0; i < data.length; i++) {
			if (data[i] != null) {
				System.out.print(i + 1 + " ");
				data[i].printProspect();
			}
		}
		//option = input.nextLine();
	}

	public static void deleteCustomer() {
		System.out.println("I am Deleting Customer");
        System.out.println("Please enter which one do you want delete:  ");
		option = input.nextLine();
		try {
			int optionInt = Integer.parseInt(option);
			if (optionInt >= 0 && optionInt < data.length) {
				Prospect[] tempData = new Prospect[data.length];
				for (int i = 0; i < data.length; i++) {

					if(optionInt == i){
						//do nothing
					}else if(optionInt > i){
						tempData[i] = data[i];
					}else{
						tempData[i-1] = data[i];
					}
				}
				data = tempData;
				System.out.println("delete complete");
			}else{
				System.err.println("Error:Number is out of range");
			}
		} catch (NumberFormatException e) {
			System.err.println("Error:It is not Number");
		}
        //option = input.nextLine();
	}

	public static void Back() {
		System.out.println("back to step before");
		option = input.nextLine();
	}

	public static void loadData() {
		try {
			System.out.println("I am loading data");
			File f = new File("Prospect.csv");
			if(!f.exists()){
				try {
					f.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			int index = 0;
			String temp;
			Scanner s = new Scanner(f);

			while(s.hasNextLine()){
				temp = s.nextLine();
				data[index] = new Prospect();
				data[index].setName(temp.substring(0, temp.indexOf(',')));
				temp = temp.substring(temp.indexOf(',')+1,temp.length());
				data[index].setHouseNumber(temp.substring(0, temp.indexOf(',')));
				temp = temp.substring(temp.indexOf(',')+1,temp.length());
				data[index].setPostcode(temp.substring(0, temp.indexOf(',')));
				temp = temp.substring(temp.indexOf(',')+1,temp.length());
				data[index].setDateContacted(temp.substring(0, temp.length()));
				index++;
			}
			
		} catch (FileNotFoundException ex) {
			System.out.println(ex.toString());
		}
	}

	public static void saveData() {
		System.out.println("I am saving data");
		File f;

		try {
			f = new File("Prospect.csv");
			PrintWriter pw = new PrintWriter(f);
			for (Prospect p : data) {
				if (p != null) {
					pw.println(p.toString());
				}
			}   
             pw.close();
		} catch (FileNotFoundException ex) {
			System.out.println("error");
		}
       
	}
}
